//
// Created by andrew on 2022/5/12.
//


#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

const pair<int, string> valueToSymbols[] = {
        {1000, "M"},
        {900, "CM"},
        {500, "D"},
        {400, "CD"},
        {100, "C"},
        {90, "XC"},
        {50, "L"},
        {40, "XL"},
        {10, "X"},
        {9, "IX"},
        {5, "V"},
        {4, "IV"},
        {1, "I"}
};


// 硬编码
const string thousands[] = {"", "M", "MM", "MMM"};
const string hundreds[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
const string tens[]      = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
const string ones[]      = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};


class Solution {
public:
    static string intToRoman(int num) {

        string roman;
        for(auto &pair : valueToSymbols) {
            while(num >= pair.first) {
                num -= pair.first;
                roman += pair.second;
            }
            if (num == 0) {
                break;
            }
        }
        return roman;
    }

    static string intToRoman2(int num) {
        return thousands[num / 1000] + hundreds[num % 1000 / 100] + tens[num % 100 / 10] + ones[num % 10];
    }

};


#include <iostream>
#include <gtest/gtest.h>

TEST(Roman, intToRoman) {

    EXPECT_STREQ("MD" , Solution::intToRoman(1500).c_str());

    EXPECT_STREQ("MMMCMXCIX" , Solution::intToRoman2(3999).c_str());
}

